package cn.b.sky.mes.cm.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.b.sky.mes.cm.model.Calendar;
import cn.b.sky.mes.cm.model.WorkCenter;
import cn.b.sky.mes.cm.model.WorkCenterType;
import cn.b.sky.mes.cm.model.WorkUnit;
import cn.b.sky.mes.cm.model.condition.WorkCenterCondition;
import cn.b.sky.mes.cm.service.CalendarService;
import cn.b.sky.mes.cm.service.WorkCenterService;
import cn.b.sky.common.constants.SystemConstant;
import cn.b.sky.common.enums.DwzCallbackType;
import cn.b.sky.common.exception.ImeException;
import cn.b.sky.common.model.DWZResult;
import cn.b.sky.user.SecurityContextUtil;
import cn.b.sky.user.controller.SecurityController;

/**
 * 
 * <strong>Title : WorkCenterController.java<br>
 * </strong> <strong>Package : cn.b.sky.mes.cm.controller<br>
 * </strong> <strong>Description : </strong>工作中心视图控制器<br>
 * <strong>Create on : 2015-9-24 下午4:10:02<br>
 * </strong>
 * <p>
 * 
 * @author lidapeng<br>
 * @version <strong>v1.0.0</strong><br>
 * <br>
 *          <strong>修改历史:</strong><br>
 *          修改人 | 修改日期 | 修改描述<br>
 *          -------------------------------------------<br>
 * <br>
 * <br>
 */
@Controller
@RequestMapping("workCenter")
public class WorkCenterController extends SecurityController {

  @Resource
  private WorkCenterService workCenterService;

  @Resource
  private CalendarService calendarService;

  @RequestMapping("workCenterTree")
  public String workCenterTree(Model model, String navTabId) {
    model.addAttribute("workCenterTree", workCenterService.getWorkCenterTree(this.getSiteCode()));
    model.addAttribute(NAVTABID, navTabId);
    return "cm/workCenter/workcenter_tree";
  }

  @RequestMapping("addWorkCenter")
  public String addWorkCenter(Model model, String navTabId, String parentWorkCenterCode) {

    if (!StringUtils.isBlank(parentWorkCenterCode)) {
      model.addAttribute("parentWorkCenterCode", parentWorkCenterCode);
      // 查询同等级工作中心类型 根据parentWorkCenterCode
      List<WorkCenterType> workTypes = workCenterService.getSameLevelTypeByWorkCenter(
          SecurityContextUtil.getSiteCode(), parentWorkCenterCode);
      model.addAttribute("workTypes", workTypes);
    } else {
      // 查询顶级工作中心类型
      List<WorkCenterType> workTypes = workCenterService.getSameLevelTypeByWorkCenter(
          SecurityContextUtil.getSiteCode(), null);
      model.addAttribute("workTypes", workTypes);
    }
    model.addAttribute(NAVTABID, navTabId);
    //工作日历
    List<Calendar> calendars = calendarService.selectAllUnlockCalendars(this.getSiteCode());
    model.addAttribute("calendars", calendars);
    return "cm/workCenter/workcenter_add";
  }

  @RequestMapping("saveWorkCenter")
  @ResponseBody
  public DWZResult saveWorkCenter(Model model, HttpServletRequest request, String navTabId, WorkCenter workCenter) {
    if (workCenter == null || StringUtils.isBlank(workCenter.getSiteCode())
        || StringUtils.isBlank(workCenter.getWorkCenterCode())) {
      return ajaxError(getImeViewMessageI18n(SecurityContextUtil.getLanguageCode(), SystemConstant.PARAMETERNULL));
    }
    setCreateInfo(workCenter);
    workCenterService.saveWorkCenter(workCenter);
    String forwardUrl = getForwardUrl(request) + "/workCenter/workCenterTree?navTabId=" + navTabId;
    return navTabAjaxDone(navTabId, null, DwzCallbackType.FORWARD, forwardUrl, null, null);
  }

  @RequestMapping("modifyWorkCenter")
  @ResponseBody
  public DWZResult modifyWorkCenter(Model model, HttpServletRequest request, String navTabId, WorkCenter workCenter) {
    if (workCenter == null || StringUtils.isBlank(workCenter.getSiteCode())
        || StringUtils.isBlank(workCenter.getWorkCenterCode())) {
      return ajaxError(getImeViewMessageI18n(SecurityContextUtil.getLanguageCode(), SystemConstant.PARAMETERNULL));
    }
    setCreateInfo(workCenter);
    workCenterService.modifyWorkCenter(workCenter);
    String forwardUrl = getForwardUrl(request) + "/workCenter/workCenterTree?navTabId=" + navTabId;
    return navTabAjaxDone(navTabId, null, DwzCallbackType.FORWARD, forwardUrl, null, null);
  }

  @RequestMapping("viewWorkCenter")
  public String viewWorkCenter(Model model, String navTabId, String workCenterCode, String siteCode) {

    if (StringUtils.isBlank(workCenterCode)) {
      throw ImeException.newException(SystemConstant.PARAMETERNULL);
    }
    if (StringUtils.isBlank(siteCode)) {
      siteCode = SecurityContextUtil.getSiteCode();
    }
    WorkCenter wc = workCenterService.getWorkCenterByPK(siteCode, workCenterCode);
    // 查询同等级工作中心类型 根据parentWorkCenterCode
    if (wc != null) {
      List<WorkCenterType> workTypes = workCenterService.getSameLevelTypeByWorkCenter(siteCode,
          wc.getParentWorkCenterCode());
      model.addAttribute("workTypes", workTypes);
    }
    model.addAttribute(NAVTABID, navTabId);
    model.addAttribute("workCenter", wc);
    //工作日历
    List<Calendar> calendars = calendarService.selectAllUnlockCalendars(this.getSiteCode());
    model.addAttribute("calendars", calendars);
    return "cm/workCenter/workcenter_view";

  }

  @RequestMapping("isExistsWorkCenter")
  @ResponseBody
  public Boolean isExistsWorkCenter(Model model, String workCenterCode) {

    if (StringUtils.isNotBlank(workCenterCode)) {
      WorkCenter wc = workCenterService.getWorkCenterByPK(SecurityContextUtil.getSiteCode(),
          StringUtils.upperCase(workCenterCode));
      if (wc != null && StringUtils.isNotBlank(wc.getWorkCenterCode())) {
        return false;
      }
    }
    return true;
  }

  @RequestMapping("deleteWorkCenter")
  @ResponseBody
  public DWZResult deleteWorkCenter(Model model, HttpServletRequest request, String navTabId, String workCenterCode) {
    if (StringUtils.isBlank(workCenterCode)) {
      return ajaxError(getImeViewMessageI18n(SecurityContextUtil.getLanguageCode(), SystemConstant.PARAMETERNULL));
    }
    //  删除前外键判断代码后续添加
    workCenterService.deleteWorkCenter(new WorkCenter(workCenterCode, SecurityContextUtil.getSiteCode()));
    return ajaxDone(null);
  }

  @RequestMapping("workCenterSuggest")
  @ResponseBody
  public Object workCenterSuggest(@ModelAttribute("condition") WorkCenterCondition condition, String hr) {
    condition.setSiteCode(getSiteCode());
    condition.setLockFlag(0);
    if (StringUtils.isBlank(condition.getWorkCenterText())) {
      condition.setWorkCenterText(condition.getWorkCenterCode());
    }
    //hr人力模块权限过滤使用
    if (!StringUtils.isBlank(hr)) {
      condition.setUserCode(SecurityContextUtil.getCurrentUserName());
    }

    return workCenterService.getWorkCenterSuggest(condition);
  }

  @RequestMapping("validateWorkCenter")
  @ResponseBody
  public boolean validateWorkCenter(Model model, String workCenterCode, String originalCode) {

    if (StringUtils.isBlank(workCenterCode)) {
      return false;
    }
    //修改校验去掉自己本事
    if (!StringUtils.isBlank(originalCode) && workCenterCode.equals(originalCode)) {
      return true;
    }

    WorkCenter wc = workCenterService.getWorkCenterByPK(SecurityContextUtil.getSiteCode(),
        StringUtils.upperCase(workCenterCode));
    if (wc != null && wc.getLockFlag() == 0) {
      return true;
    }
    return false;
  }

  @RequestMapping("validateWorkCenterForHr")
  @ResponseBody
  public boolean validateWorkCenterForHr(Model model, String workCenterCode, String originalCode) {

    if (StringUtils.isBlank(workCenterCode)) {
      return false;
    }
    //修改校验去掉自己本事
    if (!StringUtils.isBlank(originalCode) && workCenterCode.equals(originalCode)) {
      return true;
    }

    WorkCenter wc = workCenterService.getWorkCenterByPKandPermission(SecurityContextUtil.getSiteCode(),
        StringUtils.upperCase(workCenterCode), SecurityContextUtil.getCurrentUserName());
    if (wc != null && wc.getLockFlag() == 0) {
      return true;
    }
    return false;
  }

  @RequestMapping("workCenterLookup")
  public String workCenterLookup(@ModelAttribute("condition") WorkCenterCondition condition, String hr,
      String lookupType, String navTabId, Model model) {
    condition.setSiteCode(getSiteCode());
    condition.setLockFlag(0);
    //hr人力模块权限过滤使用
    if (!StringUtils.isBlank(hr)) {
      condition.setUserCode(SecurityContextUtil.getCurrentUserName());
    }
    List<WorkCenter> workCenters = workCenterService.getWorkCenterLookUp(condition);
    int count = workCenterService.getWorkCenterLookUpCount(condition);
    condition.setData(workCenters);
    condition.setTotalCount(count);
    model.addAttribute("page", condition);
    model.addAttribute(NAVTABID, navTabId);
    if (!StringUtils.isBlank(lookupType) && "radio".equals(lookupType)) {
      return "cm/workCenter/workCenter_lookup_radio"; //单选查找带回
    }
    return "cm/workCenter/workCenter_lookup";
  }
  
  @RequestMapping(value = "workCenterChange")
  @ResponseBody
  public DWZResult workCenterChange(Model model, String navTabId, String workcenterCode) {
    List<WorkUnit> units=workCenterService.selectWorkUnitByWorkCenter(getSiteCode(), workcenterCode);
    return ajaxDone(units);
  }
}
